/**
 * 文件名：****.java
 * 版权：Copyright Lightkit All Rights Reserved.
 * 描述：*****
 */
package com.lightkits.wms.domain.usuallyinterface;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.lightkits.common.TransactionValidate;
import com.lightkits.common.db.entity.FndExceptionAssign;
import com.lightkits.common.db.entity.FndExceptionBase;
import com.lightkits.common.db.entity.FndExceptionGroupBase;
import com.lightkits.common.db.entity.FndExceptionGroupLine;
import com.lightkits.common.db.entity.FndExceptionTl;
import com.lightkits.common.db.entity.FndItemBase;
import com.lightkits.framework.common.utility.object.ObjectUtil;
import com.lightkits.framework.common.utility.validate.IntegrityValidator;
import com.lightkits.framework.soa.hzero.executor.ServiceExecutor;
import com.lightkits.wms.dto.usuallyinterface.GetBadMatReaRequestType;
import com.lightkits.wms.dto.usuallyinterface.GetBadMatReaResponseType;
import com.lightkits.wms.dto.usuallyinterface.model.BadMatReasonDTO;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * GetBadMatReaExecutor
 *
 * @author jing.wei@hand-china.com
 * Created on 2019/5/16 16:13
 */
@Component
public class GetBadMatReaExecutor implements ServiceExecutor<GetBadMatReaRequestType, GetBadMatReaResponseType> {
    @Override
    public GetBadMatReaResponseType service(GetBadMatReaRequestType request) {
        GetBadMatReaResponseType response = new GetBadMatReaResponseType();
        response.setSuccessful(true);
        try {
            IntegrityValidator.begin().validate(request.getItemId(), "Item Id");

            TransactionValidate check1 = TransactionValidate.beginValidity(FndItemBase.class);
            check1.checkIdValidity(ObjectUtil.toString(request.getItemId()), "ITEM_ID");

            List<FndExceptionAssign> assigns = new FndExceptionAssign()
                    .selectList(new EntityWrapper()
                            .eq(FndExceptionAssign.ASSIGN_TYPE, "ITEM")
                            .eq(FndExceptionAssign.SOURCE_ID, request.getItemId()));
            if (ObjectUtil.isNullOrEmpty(assigns)) {
                return response;
            }
            List<Long> exceptionIds = new ArrayList<>();
            assigns.forEach(target -> {
                if (!ObjectUtil.isEmpty(target.getExceptionId())) {
                    FndExceptionBase exceptionBase = new FndExceptionBase()
                            .selectOne(new <FndExceptionBase>EntityWrapper()
                                    .eq(FndExceptionBase.ID, target.getExceptionId())
                                    .eq(FndExceptionBase.EXCEPTION_CLASS, "ITEM_SCRAP_REASON"));
                    if (!ObjectUtil.isEmpty(exceptionBase)) {
                        exceptionIds.add(exceptionBase.getId());
                    }
                }
                if (!ObjectUtil.isEmpty(target.getExceptionGroupId())) {
                    FndExceptionGroupBase groupBase = new FndExceptionGroupBase()
                            .selectById(target.getExceptionGroupId());
                    if (groupBase != null) {
                        List<FndExceptionGroupLine> lines = new FndExceptionGroupLine()
                                .selectList(new EntityWrapper()
                                        .eq(FndExceptionGroupLine.EXCEPTION_GROUP_ID, groupBase.getId()));
                        List<Long> ids = lines
                                .stream()
                                .map(FndExceptionGroupLine::getExceptionId)
                                .collect(Collectors.toList());
                        List<FndExceptionBase> fndExceptionBases = new FndExceptionBase()
                                .selectList(new EntityWrapper<FndExceptionBase>()
                                        .in(FndExceptionBase.ID, ids));
                        if (!ObjectUtil.isNullOrEmpty(fndExceptionBases)) {
                            exceptionIds.addAll(fndExceptionBases
                                    .stream()
                                    .map(FndExceptionBase::getId)
                                    .collect(Collectors.toList()));
                        }
                    }
                }
                List<BadMatReasonDTO> badMatReasonDTOS = new ArrayList<>();
                if (!ObjectUtil.isNullOrEmpty(exceptionIds)) {
                    exceptionIds.stream().distinct();
                    exceptionIds.forEach(id -> {

                        FndExceptionTl tl = new FndExceptionTl()
                                .selectOne(new <FndExceptionTl>EntityWrapper()
                                        .eq(FndExceptionTl.EXCEPTION_ID, id)
                                        .like(FndExceptionTl.EXCEPTION_NAME, request.getBadReason()));
                        if (tl != null) {
                            BadMatReasonDTO dto = new BadMatReasonDTO();
                            dto.setExceptionName(tl.getExceptionName());
                            dto.setExceptionAlias(tl.getExceptionAlias());
                            dto.setExceptionId(id.toString());
                            FndExceptionBase base = new FndExceptionBase().selectById(id);
                            dto.setExceptionCode(base.getExceptionCode());
                            badMatReasonDTOS.add(dto);
                        }
                    });
                }
                response.setPageDataCount(!ObjectUtil.isNullOrEmpty(badMatReasonDTOS)
                        ? badMatReasonDTOS.size() : 0);
                response.setBadMatReasonDTOS(badMatReasonDTOS);
            });

        } catch (Exception e) {
            response.setSuccessful(false);
            response.setMessage("get Bad Material Reason error");
        }
        return response;
    }
}
